﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using AMobiSDK;

namespace Sai_Se_Dung
{
    public partial class Ads : PhoneApplicationPage
    {
        // Constructor
        DispatcherTimer newTimer = new DispatcherTimer();
        HandleClosing handle;
        public static bool checkShow = true;
        long start = 0;
        public Ads()
        {
            InitializeComponent();
            this.progressbar.IsIndeterminate = true;
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            newTimer.Stop();
            newTimer.Tick -= OnTimerTick;
            base.OnNavigatedFrom(e);

        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

           
            //newTimer = new DispatcherTimer()
            //{
            //    Interval = new TimeSpan(0, 0, 0, 0)
            //};

            newTimer.Tick += timer_Tick;

            if (!checkShow)
            {
                progressbar.IsEnabled = false;
                NavigationService.Navigate(new Uri("/Welcome.xaml", UriKind.RelativeOrAbsolute));
            }
            handle = null;
            //nho sua lai ma code nay
            AdManager.setWidgetCode("f2257ad5570a4a773a2edd33504ef3c2");

            new AdManager().OnNavigationTo(this, null, banner, null);
            if (newTimer == null)
                newTimer = new DispatcherTimer();
            newTimer.Interval = TimeSpan.FromMilliseconds(250);
            newTimer.Start();

            newTimer.Tick -= OnTimerTick;
            newTimer.Tick += OnTimerTick;

            if (checkShow)
            {
                checkShow = false;
            }

            base.OnNavigatedTo(e);
        }

        public void OnTimerTick(object sender, EventArgs e)
        {
            if (handle == null && banner.Visibility == System.Windows.Visibility.Visible)
            {
                loading.Text = "";
                handle = new HandleClosing();
                return;
            }
            if (handle != null && banner.Visibility == System.Windows.Visibility.Collapsed)
            {
                handle = null;
                newTimer.Stop();
                NavigationService.Navigate(new Uri("/Welcome.xaml", UriKind.RelativeOrAbsolute));
            }



        }
        private void timer_Tick(object sender, EventArgs e)
        {
            //CODE LOGIC HERE
            //sau bao lau ko hien quang cao thi chuyen, o day la 40s
            start++;
            if (start > 40 && banner.Visibility != System.Windows.Visibility.Visible)
            {
                NavigationService.Navigate(new Uri("/Welcome.xaml", UriKind.Relative));
            }
        }

        
    }
}